﻿//
// Created by DengLibin on 2019/1/8 0008.
//
/**
 * 老鼠走迷宫问题
 */
#include <iostream>
using namespace std;
//数组中2表示墙壁，0表示通路，１表示走的路径
int miGong[7][7] ={
{2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2},
{2, 0, 0, 0, 0, 2, 2},
{2, 2, 0, 2, 0, 2, 2},
{2, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2}
};//迷宫图
int startX = 1, startY = 1; //起点
int endX = 5, endY = 5; //终点
bool arriveEnd =false;//是否到达终点
/**
 * 寻找路线
 * @param x 当前位置 x
 * @param y  当前位置 y
 * @return
 *
 */
bool find(int x, int y){
    miGong[x][y] = 1; //置为1
    if(x == endX && y == endY){ //到达终点
       arriveEnd = true;
    }

    //往右走
    if(miGong[x][y+1] == 0 && arriveEnd == false){
        find(x, y+1);
    }
    //往左走
    if(miGong[x][y-1] == 0 && arriveEnd == false){
        find(x, y-1);
    }
    //往下走
    if(miGong[x+1][y] == 0 && arriveEnd == false){
        find(x+1, y);
    }
    //往上走
    if(miGong[x-1][y] == 0 && arriveEnd == false){
        find(x-1, y);
    }


    if(arriveEnd == false){ //走不通
        miGong[x][y] = 0;
    }
    return arriveEnd;

}

 int main47(){

     int i,j;
     printf("显示迷宫：\n");
     for(i=0;i<7;i++)
     {
         for(j=0;j<7;j++)
             if(miGong[i][j]==2)
                 printf("██");
             else
                 printf("  ");
         printf("\n");
     }
     if(find(startX,startY) == false)
     {
         printf("\n没有找到出口！\n");
     }
     else
     {
         printf("\n显示路径:\n");
         for(i=0;i<7;i++)
         {
             for(j=0;j<7;j++)
             {
                 if(miGong[i][j]==2)
                     printf("██");
                 else if(miGong[i][j]==1)
                     printf("* ");
                 else
                     printf("  ");
             }
             printf("\n");
         }
     }
     return 0;
 }
